// pages/login/login.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginLogo: 'http://maint.duofriend.com/upload//image/3/gt123/3/20170905/FB8FA35CDA420BAB84AE9101EA396F93.png',
    inputName: 'http://maint.duofriend.com/upload//image/3/gt123/3/20170905/3651C9FC3C205EDD2D46F4DEED6B186D.png',
    inputPass: 'http://maint.duofriend.com/upload//image/3/gt123/3/20170905/6F4B2EF110710042D1DB9A79C5FD84D9.png',
    loginDis: false,
    getDis: false,
    hasUserInfo: false,
    login_name:'',
    password:''
  },

  loginName: function(e){
    var that = this;
    that.data.login_name = e.detail.value;
    console.log(that.data.login_name)
  },

  loginPass: function(e){
    var that = this;
    that.data.password = e.detail.value;
    console.log(that.data.password)
  },

  getUserInfo: function(e){
      var that = this;
      wx.showLoading({
        title: '授权中',
      });
      utils.login(app,function(){
        console.log("memberId===", wx.getStorageSync("memberId"))
        wx.request({
          url: app.globalData.http + "authorization.do",
          data:{
            memberId: parseInt(wx.getStorageSync("memberId"))
          },
          success: function(res){
            wx.hideLoading()
            var code = res.data.code;
            console.log("请求授权--",res);
            if(code == 100){
              wx.navigateTo({
                url: '/pages/index/index',
              });
              wx.setStorageSync('busId', res.data.data.busId);
              wx.setStorageSync('employeesId', res.data.data.employeesId);
              wx.setStorageSync('schoolId', res.data.data.schoolId);
            }else if(code == 200){
              console.log("此时应该进入登录页面");
              return false;
            }else{
              wx.showModal({
                title: '提示',
                 content: '无权限进入',
                showCancel: false,
              });
              return false;
            }
          },
          fail: function(){
            console.log("接口请求出错");
            return false
          }
        })
      })
  },

  formSubmit: function (e) {
    var that = this;
    var memberId = parseInt(wx.getStorageSync("memberId"));
    wx.showLoading({
      title: '登录中',
    });
    console.log("member==", parseInt(wx.getStorageSync("memberId")));
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    if (memberId){
        utils.login(app, function () {
        wx.request({
          url: app.globalData.http + "login.do",
          data: {
            login_name: e.detail.value.loginName,
            password: e.detail.value.loginPass,
            memberId: memberId
          },
          success: function (res) {
            var code = res.data.code;
            console.log("直接登录成功返回的res====",res.data);
            wx.hideLoading();
            if (code == 100) {
              wx.navigateTo({
                url: '/pages/index/index',
              });
            } else if (code == 200) {
              wx.showModal({
                title: '提示',
                content: '登录失败，账号或者密码错误',
                showCancel: false,
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '无权限操作',
                showCancel: false,
              })
            }
          }
        })
      })
    }else{
      utils.login(app, function () {
        console.log("e.detail.value.loginName", e.detail.value.loginName);
        console.log("e.detail.value.loginPass", e.detail.value.loginPass);
        wx.request({
          url: app.globalData.http + "login.do",
          data: {
            login_name: e.detail.value.loginName,
            password: e.detail.value.loginPass
          },
          success: function (res) {
            wx.hideLoading()
            var code = res.data.code;
            wx.hideLoading()
            console.log("直接登录返回的数据：", res.data);
            if (code == 100) {
              wx.navigateTo({
                url: '/pages/index/index',
              });
              wx.setStorageSync('busId', res.data.data.busId);
              wx.setStorageSync('employeesId', res.data.data.employeesId);
              wx.setStorageSync('schoolId', res.data.data.schoolId);
            } else if (code == 200) {
              wx.showModal({
                title: '提示',
                content: '登录失败，账号或者密码错误',
                showCancel: false,
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '无权限操作',
                showCancel: false,
              })
            }
          }
        })
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})